<?php

namespace app\admin\controller\order;

use app\common\controller\Backend;
use think\process\exception\Failed;

class Order extends Backend{
    public function __construct()
    {
        parent::__construct();

        $this->UserModel=model('User.User');
        $this->AddressModel=model('User.Address');
        $this->OrderModel=model('Order.Order');
        $this->OrderProductModel=model('Order.Product');
        $this->ProductModel=model('Product.Product');
        $this->ExpressModel=model('Express');
    }

    public function index(){
        $orderlist=$this->OrderModel->with(['users'])->paginate(10);

        $count=$this->OrderModel->count();

        $pagestr=$orderlist->render();

        $this->assign([
            'orderlist'=>$orderlist,
            'count'=>$count,
            'pagestr'=>$pagestr,
        ]);

        return $this->fetch();
    }

    public function info(){
        $orderid=$this->request->param('orderid',0);

        $order=$this->OrderModel->find($orderid);

        if(!$order){
            $this->error('该订单不存在');
            exit;
        }

        $OrderProduct=$this->OrderProductModel->where(['orderid'=>$orderid])->find();

        $address=$this->AddressModel->with(['provinces','citys','districts'])->where(['id'=>$order['addrid']])->find();

        $express=$this->ExpressModel->where(['id'=>$order['expid']])->find();

        $this->assign([
            'OrderProduct'=>$OrderProduct,
            'order'=>$order,
            'address'=>$address,
            'express'=>$express,

        ]);

        return $this->fetch();
    }

    public function delivery(){
        $orderid=$this->request->param('orderid',0);

        $order=$this->OrderModel->find($orderid);

        if(!$order){
            $this->error('该订单不存在');
            exit;
        }

        $express=$this->ExpressModel->select();

        if($this->request->isPost()){
            $expid=$this->request->param('expid',0);

            $this->ProductModel->startTrans();
            $this->OrderModel->startTrans();

            $exp=$this->ExpressModel->find($expid);
            $OrderData=[
                'id'=>$orderid,
                'expid'=>$expid,
                'status'=>2
            ];

            $OrderData['expcode']=build_code($exp['prefix']);

            $OrderStatus=$this->OrderModel->isUpdate(true)->save($OrderData);

            if($OrderStatus===FALSE){
                $this->error($this->OrderModel->getError());
                exit;
            }

            $OrderProduct=$this->OrderProductModel->where(['orderid'=>$orderid])->find();

            $Product=$this->ProductModel->where(['id'=>$OrderProduct['proid']])->find();

            $stock=bcsub($Product['stock'],$OrderProduct['nums']);

            $ProductData=[
                'id'=>$OrderProduct['proid'],
                'stock'=>$stock
            ];

            $ProductStatus=$this->ProductModel->isUpdate(true)->save($ProductData);

            if($ProductStatus===FALSE){
                $this->OrderModel->rollback();
                $this->error($this->ProductModel->getError());
                exit;
            }

            if($OrderStatus===FALSE || $ProductStatus===FALSE){
                $this->ProductModel->rollback();
                $this->OrderModel->rollback();
                $this->error('发货失败');
                exit;
            }else{
                $this->ProductModel->commit();
                $this->OrderModel->commit();
                $this->success('发货成功',url('admin/order.order/index'));
                exit;
            }
        }

        $this->assign([
            'express'=>$express
        ]);

        return $this->fetch();
    }
}